import Cookies from 'js-cookie'
import CONSTANTS from '@/config/constants'
import util from '@/libs/util'
import mixin from '../mixin'

console.log(CONSTANTS.UPLOAD_FILE)
console.log(CONSTANTS.DIPLOMAS_SAVE)

export default {
    props: ['baseuser'],
    mixins: [mixin],
    data() {
        return {
            generateData: {
                name: '',
                orgId: -1,
                cover: ''
            },
            orgShow: false,
            uploaded: false,
            fileUploadUrl: CONSTANTS.UPLOAD_FILE,
            SERVER_HOST: CONSTANTS.SERVER_HOST
        }
    },
    created() {
        if (this.baseuser.orgId === 0) {
            this.orgShow = true
        }
    },
    methods: {
        handleSuccess(res, file) {
            this.uploaded = true
            this.generateData.cover = ''

            var xmlhttp = null;
            var urlPath = CONSTANTS.SERVER_HOST + res.data.imgUrl;
            if (window.ActiveXObject) {
                xmlhttp = new ActiveXObject("Microsoft.XMLDOM");
            } else {
                if (window.XMLHttpRequest) {
                    xmlhttp = new XMLHttpRequest();
                }
            }
            let interval = setInterval(() => {
                xmlhttp.open('GET', urlPath, false);
                xmlhttp.send();
                if ((xmlhttp.readyState === 4 && xmlhttp.status === 200)) {
                    clearInterval(interval);
                    this.$Notice.success({
                        title: '文件上传成功',
                        desc: '文件 ' + file.name + ' 上传成功。',
                        duration: 2
                    });
                    this.generateData.cover = res.data.imgUrl
                    // file.url = CONSTANTS.SERVER_HOST + res.data.imgUrl;
                    // file.name = res.data.fileName;
                    // this.uploadList = this.$refs.upload.fileList;
                    // this.formData.cover = res.data.imgUrl;
                    // this.imgUrl = res.data.imgUrl;
                }
            }, 500);
        },
        handleFormatError(file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请选择图片文件。',
                duration: 5
            });
        },
        handleMaxSize(file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。',
                duration: 5
            });
        },
        handleRemove(file) {
            this.uploadList = [];
            this.$refs.upload.fileList = [];
            this.formData.cover = '';
        },
        async save() {
            const req = Object.assign({}, this.generateData)
            if (this.baseuser.type === 2) {
                req.orgId = this.baseuser.orgId
            }
            const res = await util.ajax.post(CONSTANTS.SEAL_SAVE, util.toFormData(req))
            return res.data.message === 'SUCCESS'
        }
    },
    computed: {
    },
    beforeDestroy() {
    }
};